#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include "global.h"
#include "macro.h"
#include "utils/utils.h"

#include <QApplication>
#include <QFontDialog>
#include <QColorDialog>
#include <QFileDialog>
#include <QCloseEvent>

#ifdef Q_OS_WIN
    #include <windows.h>
#endif

SettingsDialog::SettingsDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::SettingsDialog)
{
    this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);
    ui->setupUi(this);
    ui->stacked->setCurrentIndex(0);
}

SettingsDialog::~SettingsDialog()
{
    delete ui;
}

void SettingsDialog::initData()
{
    // 文字水印配置
    ui->edit_textcontent->setPlainText(GL.textContent());
    ui->btn_textfont->setText(QString("%1 , %2").arg(GL.textFont().family(), QString::number(GL.textFont().pointSize())));
    ui->btn_textcolor->setStyleSheet(QString("background-color:%1;").arg(GL.textColor().name()));
    ui->sli_textopacity->setValue(GL.textColor().alpha());
    ui->sli_textslope->setValue(GL.textSlope());
    ui->sli_textdensity->setValue(GL.textDensity());

    // 图片水印配置
    QPixmap imagePixmap = GL.imagePixmap();
    if (imagePixmap.isNull())
    {
        ui->lab_imageview->setText("图片不正确");
    }
    else
    {
        ui->lab_imageview->setPixmap(imagePixmap.scaled(ui->lab_imageview->maximumSize(), Qt::KeepAspectRatio));
    }
    ui->sli_imageopacity->setValue(GL.imageOpacity());
    ui->sli_imageslope->setValue(GL.imageSlope());
    ui->sli_imagescale->setValue(GL.imageScale());
    ui->cbb_imagepos->setCurrentIndex(GL.imagePos());

    // 二维码水印配置
    ui->edit_qrcontent->setPlainText(GL.qrContent());
    ui->btn_qrcolor->setStyleSheet(QString("background-color:%1;").arg(GL.qrColor().name()));
    ui->sli_qropacity->setValue(GL.qrOpacity());
    ui->sli_qrslope->setValue(GL.qrSlope());
    ui->sli_qrsize->setValue(GL.qrSize());
    ui->cbb_qrpos->setCurrentIndex(GL.qrPos());

    // 暗水印配置
    ui->edit_darkcontent->setPlainText(GL.darkContent());
    ui->btn_darkfont->setText(QString("%1 , %2").arg(GL.darkFont().family(), QString::number(GL.darkFont().pointSize())));
    ui->btn_darkcolor->setStyleSheet(QString("background-color:%1;").arg(GL.darkColor().name()));
    ui->sli_darkopacity->setValue(GL.darkOpacity());
    ui->sli_darkslope->setValue(GL.darkSlope());
    ui->sli_darkdensity->setValue(GL.darkDensity());

    // 点阵水印配置
    ui->edit_matrixcontent->setValidator(new QRegExpValidator(QRegExp("^[0-9a-fA-F]+$")));
    QString matrixContent = GL.matrixContent();
    ui->edit_matrixcontent->setText(matrixContent.isEmpty() ? "01234567" : matrixContent);
    ui->btn_matrixcolor->setStyleSheet(QString("background-color:%1;").arg(GL.matrixColor().name()));
    ui->sli_matrixdiam->setValue(GL.matrixDiam());
    ui->sli_matrixopacity->setValue(GL.matrixOpacity());
    ui->sli_matrixlength->setValue(GL.matrixLength());
    ui->sli_matrixinterval->setValue(GL.matrixInterval());
}

void SettingsDialog::showWindow()
{
    if (isMinimized())
    {
        showNormal();
    }
#ifdef Q_OS_WIN
    ::SetWindowPos(HWND(winId()), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
    ::SetWindowPos(HWND(winId()), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
#endif
    show();
    activateWindow();
}

void SettingsDialog::closeEvent(QCloseEvent *event)
{
    hide();
    event->ignore();
}

void SettingsDialog::showEvent(QShowEvent *event)
{
    QDialog::showEvent(event);
}

void SettingsDialog::on_cbb_watertype_currentIndexChanged(int index)
{
    ui->stacked->setCurrentIndex(index);
}

void SettingsDialog::on_btn_apply_clicked()
{
    switch (ui->cbb_watertype->currentIndex())
    {
        case 0:
            GL.setWaterType(WATER_TYPE_TEXT);
            break;
        case 1:
            GL.setWaterType(WATER_TYPE_IMAGE);
            break;
        case 2:
            GL.setWaterType(WATER_TYPE_QRCODE);
            break;
        case 3:
            GL.setWaterType(WATER_TYPE_DARK);
            break;
        case 4:
            GL.setWaterType(WATER_TYPE_MATRIX);
            break;
        default:
            return;
    }
    ui->lab_msg->setText("当前显示水印：" + ui->cbb_watertype->currentText());
    emit updateWatermarkWindow();
}

void SettingsDialog::on_btn_clear_clicked()
{
    GL.setWaterType(0);
    ui->lab_msg->setText("当前桌面无水印显示");
    emit updateWatermarkWindow();
}

void SettingsDialog::on_sli_textopacity_valueChanged(int value)
{
    ui->lab_textopacity->setText(QString::number(value));
    GL.setTextOpacity(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_sli_textslope_valueChanged(int value)
{
    ui->lab_textslope->setText(QString::number(value));
    GL.setTextSlope(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_sli_textdensity_valueChanged(int value)
{
    ui->lab_textdensity->setText(QString::number(value));
    GL.setTextDensity(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_btn_textfont_clicked()
{
    QFontDialog fontDialog;
    fontDialog.setWindowTitle("选择字体");
    fontDialog.setCurrentFont(GL.textFont());
    connect(&fontDialog, &QFontDialog::fontSelected, this, [ = ](const QFont & font)
    {
        ui->btn_textfont->setText(QString("%1， %2").arg(font.family(), QString::number(font.pointSize())));
        GL.setTextFont(font);
        emit updateWatermarkWindow();
    });
    fontDialog.exec();
}

void SettingsDialog::on_btn_textcolor_clicked()
{
    QColorDialog colorDialog;
    colorDialog.setWindowTitle("选择颜色");
    colorDialog.setCurrentColor(GL.textColor());
    connect(&colorDialog, &QColorDialog::colorSelected, this, [ = ](const QColor & color)
    {
        ui->btn_textcolor->setStyleSheet(QString("background-color:%1;").arg(color.name()));
        GL.setTextColor(color);
        emit updateWatermarkWindow();
    });
    colorDialog.exec();
}

void SettingsDialog::on_edit_textcontent_textChanged()
{
    QString content = ui->edit_textcontent->toPlainText();
    GL.setTextContent(content);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_btn_imageselect_clicked()
{
    QFileDialog fileDialog;
    fileDialog.setWindowTitle("选择图片");
    fileDialog.setDirectory(PathUtil::picturesDir());
    fileDialog.setNameFilter("Images(*.png *.jpg)");
    fileDialog.setViewMode(QFileDialog::Detail);
    connect(&fileDialog, &QFileDialog::fileSelected, this, [ = ](const QString & file)
    {
        // 显示预览图片
        QPixmap pixmap(file);
        if (pixmap.isNull())
        {
            ui->lab_imageview->setText("图片不正确");
        }
        else
        {
            pixmap = pixmap.scaled(ui->lab_imageview->maximumSize(), Qt::KeepAspectRatio);
            ui->lab_imageview->setPixmap(pixmap);
        }

        GL.setImagePixmap(file);
        emit updateWatermarkWindow();
    });
    fileDialog.exec();
}

void SettingsDialog::on_sli_imageopacity_valueChanged(int value)
{
    ui->lab_imageopacity->setText(QString::number(value));
    GL.setImageOpacity(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_sli_imageslope_valueChanged(int value)
{
    ui->lab_imageslope->setText(QString::number(value));
    GL.setImageSlope(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_sli_imagescale_valueChanged(int value)
{
    ui->lab_imagescale->setText(QString::number(value));
    GL.setImageScale(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_cbb_imagepos_currentIndexChanged(int index)
{
    GL.setImagePos(index);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_edit_qrcontent_textChanged()
{
    QString content = ui->edit_qrcontent->toPlainText();
    GL.setQrContent(content);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_btn_qrcolor_clicked()
{
    QColorDialog colorDialog;
    colorDialog.setWindowTitle("选择颜色");
    colorDialog.setCurrentColor(GL.qrColor());
    connect(&colorDialog, &QColorDialog::colorSelected, this, [ = ](const QColor & color)
    {
        ui->btn_qrcolor->setStyleSheet(QString("background-color:%1;").arg(color.name()));
        GL.setQrColor(color);
        emit updateWatermarkWindow();
    });
    colorDialog.exec();
}

void SettingsDialog::on_sli_qropacity_valueChanged(int value)
{
    ui->lab_qropacity->setText(QString::number(value));
    GL.setQrOpacity(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_sli_qrslope_valueChanged(int value)
{
    ui->lab_qrslope->setText(QString::number(value));
    GL.setQrSlope(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_sli_qrsize_valueChanged(int value)
{
    ui->lab_qrsize->setText(QString::number(value));
    GL.setQrSize(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_cbb_qrpos_currentIndexChanged(int index)
{
    GL.setQrPos(index);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_edit_darkcontent_textChanged()
{
    QString content = ui->edit_darkcontent->toPlainText();
    GL.setDarkContent(content);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_btn_darkfont_clicked()
{
    QFontDialog fontDialog;
    fontDialog.setWindowTitle("选择字体");
    fontDialog.setCurrentFont(GL.darkFont());
    connect(&fontDialog, &QFontDialog::fontSelected, this, [ = ](const QFont & font)
    {
        ui->btn_darkfont->setText(QString("%1， %2").arg(font.family(), QString::number(font.pointSize())));
        GL.setDarkFont(font);
        emit updateWatermarkWindow();
    });
    fontDialog.exec();
}

void SettingsDialog::on_btn_darkcolor_clicked()
{
    QColorDialog colorDialog;
    colorDialog.setWindowTitle("选择颜色");
    colorDialog.setCurrentColor(GL.darkColor());
    connect(&colorDialog, &QColorDialog::colorSelected, this, [ = ](const QColor & color)
    {
        ui->btn_darkcolor->setStyleSheet(QString("background-color:%1;").arg(color.name()));
        GL.setDarkColor(color);
        emit updateWatermarkWindow();
    });
    colorDialog.exec();
}

void SettingsDialog::on_sli_darkopacity_valueChanged(int value)
{
    ui->lab_darkopacity->setText(QString::number(value));
    GL.setDarkOpacity(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_sli_darkslope_valueChanged(int value)
{
    ui->lab_darkslope->setText(QString::number(value));
    GL.setDarkSlope(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_sli_darkdensity_valueChanged(int value)
{
    ui->lab_darkdensity->setText(QString::number(value));
    GL.setDarkDensity(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_sli_matrixdiam_valueChanged(int value)
{
    ui->lab_matrixdiam->setText(QString::number(value));
    GL.setMatrixDiam(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_btn_matrixcolor_clicked()
{
    QColorDialog colorDialog;
    colorDialog.setWindowTitle("选择颜色");
    colorDialog.setCurrentColor(GL.matrixColor());
    connect(&colorDialog, &QColorDialog::colorSelected, this, [ = ](const QColor & color)
    {
        ui->btn_matrixcolor->setStyleSheet(QString("background-color:%1;").arg(color.name()));
        GL.setMatrixColor(color);
        emit updateWatermarkWindow();
    });
    colorDialog.exec();
}

void SettingsDialog::on_sli_matrixopacity_valueChanged(int value)
{
    ui->lab_matrixopacity->setText(QString::number(value));
    GL.setMatrixOpacity(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_sli_matrixlength_valueChanged(int value)
{
    ui->lab_matrixlength->setText(QString::number(value));
    GL.setMatrixLength(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_sli_matrixinterval_valueChanged(int value)
{
    ui->lab_matrixinterval->setText(QString::number(value));
    GL.setMatrixInterval(value);
    emit updateWatermarkWindow();
}

void SettingsDialog::on_edit_matrixcontent_textChanged(const QString &text)
{
    if (text.isEmpty() || text.length() != 8)
    {
        ui->lab_matrixerr->setVisible(true);
        return;
    }
    ui->lab_matrixerr->setVisible(false);
    GL.setMatrixContent("*" + text);
    emit updateWatermarkWindow();
}


